<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
    <%@ include file="../base/top.jsp" %>
    <%--<link rel="stylesheet" href="static/zTree/css/demo.css" />--%>
    <link rel="stylesheet" href="static/zTree/css/zTreeStyle/zTreeStyle.css" />
    <script type="text/javascript" src="static/zTree/js/ztree-jquery-1.4.4.min.js"></script>
    <script type="text/javascript" src="static/zTree/js/jquery.ztree.core.js"></script>
    <script type="text/javascript" src="static/zTree/js/jquery.ztree.excheck.js"></script>
    <%--级联勾选--%>
    <script type="text/javascript" src="static/zTree/js/my-ztree.js"></script>
    <title>Title</title>
    <style>
        .ztree-div{
            width: 100%;
            text-align: center;
            height: 430px;
            overflow-y: auto;
            margin-bottom: 10px;
        }
        .ztree{
            font-size: 18px;
            margin-left: 50px;
        }
        ul,li{
            margin-top: 13px;
        }

        .ztree-bottom {
            height: 60px;
            width: 100%;
            text-align: center;
        }
    </style>
</head>
<body>

    <div class="ztree-div">
        <ul id="tree" class="ztree"></ul>
    </div>

    <div class="ztree-bottom">
        <input type="text" name="authorityId" id="role-id" style="display: none"/>
        <input type="text" name="success" id="success" style="display: none"/>
        <input type="text" name="msg" id="msg" style="display: none"/>
        <input type="text" id="role-name">
        <button id="save-btn" class="btn btn-primary" onclick="save()">保存</button>
    </div>
    <%--ztree级联勾选单独分包了--%>
    <script>
        var jsonStr =${allAuthorityInfosJson};
        var zNodes;
        zNodes =eval(jsonStr);
        $(document).ready(function(){
            $.fn.zTree.init($("#tree"), setting, zNodes);
        });
    </script>

    <script>
        $(document).ready(function(){
            if('${dotype}'=='edit'){
                <c:if test="${not empty roleDetail}">
                    $("#role-id").val("${roleDetail.roleId}");
                    $("#role-name").val("${roleDetail.roleName}");
                    checkRoleAuthority('${authoritysJson}');
                </c:if>
            }
        });

        function save() {
            var roleDetail =getRoleDetail();
            $.ajax({
                type:"POST",
                url:"<%=basePath%>role/${dotype}",
                dataType: "json",
                data:JSON.stringify(roleDetail),
                contentType:"application/json",

                success:function (data) {
                    $("#success").val(data["success"]);
                    $("#msg").val(data["msg"]);
                    //关闭一定要写在ajax执行完成之后
                    top.Dialog.close();
                }
            });

        }
        /**
         * 拼装RoleDetail对象
         * @return Json RoleDetail 对象
         * @create on 2018/12/21 by johnny
         */
        function getRoleDetail() {
            var roleDetail ={};
            roleDetail.roleId =$("#role-id").val();
            roleDetail.roleName = $("#role-name").val();
            var authorityArray = [];

            var treeObj = $.fn.zTree.getZTreeObj("tree");
            //获取所有被选中的节点
            //                  注：getCheckedNodes() 不能获取不可编辑状态的节点
            var checkNodes = treeObj.getNodesByParam("checked", true, null);;
            var nodes=new Array();
            for(var i=0;i<checkNodes.length;i++){
                //如果该节点是不是父节点，即 不是分类节点
                if(checkNodes[i].isParent != true){
                    nodes.push(checkNodes[i]);
                }
            }
            roleDetail.authorityInfos = nodes;
            return roleDetail;
        }
        /**
         * 勾选该角色已经拥有的权限
         *
         * @param data 该角色拥有权限的json字符床
         * @create on 2018/12/21 by johnny
         */
        function checkRoleAuthority(data) {
            //如果json 字符串不为空
            if(data!=null && data!=""){
                //转化为json对象
                var json = JSON.parse(data);
                //json对象不为空，且数组长度大于0
                if(json!=null && json.length>0){
                    var treeObj = $.fn.zTree.getZTreeObj("tree");
                    for(var i=0;i<json.length;i++){
                        //根据authorityId 在tree中查找节点
                        var node = treeObj.getNodeByParam("authorityId",json[i].authorityId,null);
                        //如果找到了节点
                        if(node!=null && node.isParent != true){
                            //设置该节点为选中状态
                            treeObj.checkNode(node, true, true,true);
                        }
                    }
                }
            }
        }

        /**
         * 检查表单格式
         *
         * @create on 2018/11/27 by johnny
         * @update on 2018/12/27 by johnny
         */
        function checkFormats() {
            if($("#role-name").val()==null || $("#role-name").val()==""){
                top.operateError("标签库名不能为空");
                $("#role-name").focus();
                return false;
            }
            var reg =/^[\u4e00-\u9fa5]{1,8}$/g;
            if(reg.test($("#role-name").val())){
                return true;
            }else{
                top.operateError("角色名必须为1-8个中文");
                $("#role-name").focus();
                return false;
            }
            return false;
        }

    </script>
</body>
</html>
